<template>
	<view class="notification">
		<view class="box-list">
			<view class="_for" @click="$_to('notification/system')">
				<text class="iconfont icon-shezhi"></text>
				<view class="_for-c">
					<text class="text">{{$t('systemNotifications')}}</text>
					<text
						class="digit"
						v-if="systemNotification">{{systemNotification}}</text>
				</view>
				<view class="_for-all-button" @click.stop="clickReadStatusButton(1)">
					已阅
				</view>
				<text class="iconfont icon-arrow-double-right"></text>
			</view>
			<view class="_for" @click="$_to('notification/mention')">
				<text class="iconfont icon-shequ"></text>
				<view class="_for-c">
					<text class="text">{{$t('notificationMentioned')}}</text>
					<text class="digit" v-if="mentionNotification">{{mentionNotification}}</text>
				</view>
				<view class="_for-all-button" @click.stop="clickReadStatusButton(7)">
					已阅
				</view>
				<text class="iconfont icon-arrow-double-right"></text>
			</view>
			<view class="_for" @click="$_to('notification/comment')">
				<text class="iconfont icon-pinglun"></text>
				<view class="_for-c">
					<text class="text">{{$t('notificationCommented')}}</text>
					<text
						class="digit"
						v-if="commentNotification">{{commentNotification}}</text>
				</view>
				<view class="_for-all-button" @click.stop="clickReadStatusButton(8)">
					已阅
				</view>
				<text class="iconfont icon-arrow-double-right"></text>
			</view>
		</view>
		<!-- 即时聊天列表 -->
		<view class="notification-chat">
			<template v-if="listConversationStatus === 'success'">
				<view
					class="_for"
					v-for="item in listConversation"
					@click="$_to('notification/conversation?uid=' + item.user.uid)">
					<view class="_avatar"><image :src="item.user.avatar" mode="widthFix"></image></view>
					<view class="_for-c">
						<view class="_top">
							<text>{{item.user.nickname}}</text>
							<text class="_top-time">{{item.latestMessage.timeAgo}}</text>
						</view>
						<view class="_bottom" v-if="item.latestMessage.message">
							{{$_filterHtml(item.latestMessage.message)}}
						</view>
					</view>
					<view class="_for-r">
						<view class="_red" v-if="item.unreadCount !== 0"></view>
						<view
							class="_del-button"
							@click.stop="clickConversationDeleteButton(item.user.uid)">{{$t('delete')}}</view>
					</view>
				</view>
				<view class="more-content">
					{{listConversationPageText}}
				</view>
			</template>
			<template v-if="listConversationStatus === 'loading'">
				<view style="padding: 40rpx 0 25rpx 0;"><global-loading></global-loading></view>
			</template>
			<template v-if="listConversationStatus === 'empty'"></template>
		</view>
		<view class="all-read-status" @click="clickAllReadStatusButton">
			<text class="iconfont icon-hide"></text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listConversation:[],
				listConversationStatus:"loading",
				listConversationPageText:this.$t('scrollUpToLoadMore'),
				listConversationPage:2,
				systemNotification:"",
				mentionNotification:"",
				commentNotification:"",
			}
		},
		onShow() {
			if(this.$_isLogin()){
				// 是否有未读
				this.$_isNotification();
				// 获取系统通知
				this.getNotification_1();
			}else{
				uni.switchTab({
					url:"/pages/my/my"
				})
			}
		},
		onLoad() {
			
		},
		methods: {
			// 获取系统通知
			getNotification_1(){
				this.$_request(
					"GET","/api/fresns/v1/notification/list",
					{
						"status":0,
						"types":1,
						"filterUserType":"whitelist",
						"filterUserKeys":"uid",
						"filterInfoType":"whitelist",
						"filterInfoKeys":"pid",
					},
					(res) => {
						this.systemNotification = res.data.data.pagination.total;
						this.getNotification_7();
					}
				)
			},
			getNotification_7(){
				this.$_request(
					"GET","/api/fresns/v1/notification/list",
					{
						"status":0,
						"types":7,
						"filterUserType":"whitelist",
						"filterUserKeys":"uid",
						"filterInfoType":"whitelist",
						"filterInfoKeys":"pid",
					},
					(res) => {
						this.mentionNotification = res.data.data.pagination.total;
						this.getNotification_8();
					}
				)
			},
			
			getNotification_8(){
				this.$_request(
					"GET","/api/fresns/v1/notification/list",
					{
						"status":0,
						"types":8,
						"filterUserType":"whitelist",
						"filterUserKeys":"uid",
						"filterInfoType":"whitelist",
						"filterInfoKeys":"pid",
					},
					(res) => {
						this.commentNotification = res.data.data.pagination.total;
						// 获取对话
						this.getConversation();
					}
				)
			},
			
			
			// 获取对话
			getConversation(){
				this.$_request(
					"GET","/api/fresns/v1/conversation/list",
					{
						"filterUserType":"whitelist",
						"filterUserKeys":"avatar,nickname,uid",
					},
					(res) => {
						if (res.data.data.list.length) {
							this.listConversation = res.data.data.list;
							this.listConversationStatus = "success";
						} else {
							this.listConversationStatus = "empty";
						}
					}
				)
			},
			// 删除对话
			clickConversationDeleteButton(uid){
				uni.showModal({
					title: this.$t('tip'),
					content: this.$t('delete') + '?',
					success: (res) => {
						if (res.confirm) {
							this.$_request(
								"DELETE",`/api/fresns/v1/conversation/${uid}`,
								{
									"type":"conversation"
								},
								(ref) => {
									// 获取对话
									this.getConversation();
								}
							)
						}
					}
				});
			},
			clickReadStatusButton(type){
				uni.showLoading();
				this.$_request(
					"PATCH","/api/fresns/v1/notification/read-status",
					{
						"type":"all",
						"notificationType":type
					},
					(res) => {
						uni.hideLoading();
						if(res.data.code === 0){
							this.getNotification_1();
						}else{
							uni.showToast({
								title: res.data.message,
								duration: 1500
							});
						}
					}
				)
			},
			clickAllReadStatusButton(){
				uni.showLoading();
				this.$_request(
					"PATCH","/api/fresns/v1/notification/read-status",
					{
						"type":"all"
					},
					(res) => {
						uni.hideLoading();
						if(res.data.code === 0){
							// 是否有未读
							this.$_isNotification();
							// 获取系统通知
							this.getNotification_1();
						}else{
							uni.showToast({
								title: res.data.message,
								duration: 1500
							});
						}
					}
				)
			},
		},
		// 上拉加载事件
		onReachBottom() {
			if (this.listConversationPageText == this.$t('loading')) { return }
			this.listConversationPageText = this.$t('loading')
			this.$_request(
				"GET","/api/fresns/v1/conversation/list",
				{
					"filterUserType":"whitelist",
					"filterUserKeys":"avatar,nickname,uid",
					"page":this.listConversationPage
				},
				(res) => {
					if (res.data.data.list.length) {
						this.listConversation = this.listConversation.concat(res.data.data.list);
						this.listConversationPage++
						this.listConversationPageText = this.$t('scrollUpToLoadMore')
					} else {
						this.listConversationPageText = this.$t('listWithoutPage')
					}
				}
			)
		},
		// 下拉刷新
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
		}
	}
</script>

<style lang="scss">
.notification{
	.box-list{
		background: #FFF;
		padding: 0 25rpx;
		._for{
			display: flex;
			align-items: center;
			border-bottom: 1px solid $background;
			padding: 25rpx 0;
			.iconfont{
				display: flex;
				align-items: center;
				flex-shrink: 0;
			}
			._for-c{
				display: flex;
				align-items: center;
				flex: 1;
				margin-left: 10rpx;
				.digit{
					display: flex;
					align-items: center;
					font-size: 22rpx;
					line-height: 22rpx;
					background: $red;
					color: #FFF;
					padding: 5rpx 10rpx;
					border-radius: 100rpx;
					margin-left: 10rpx;
				}
			}
			._for-all-button{
				font-size: 22rpx;
				line-height: 22rpx;
				padding-right: 15rpx;
				color: $placeholder;
			}
			.icon-arrow-double-right{
				display: flex;
				align-items: center;
				color: $placeholder;
				font-size: 28rpx;
			}
		}
		._for:last-child{
			border-bottom: 0;
		}
	}
	.notification-chat{
		padding: 0 25rpx;
		margin-top: 25rpx;
		background: #FFF;
		._for{
			display: flex;
			align-items: center;
			padding: 25rpx 0;
			border-bottom: 1px solid $background;
			._avatar{
				width: 90rpx;
				height: 90rpx;
				overflow: hidden;
				flex-shrink: 0;
				border-radius: 100rpx;
			}
			._for-c{
				flex: 1;
				margin: 0 15rpx;
				._top{
					display: flex;
					align-items: center;
					line-height: 28rpx;
					margin-bottom: 15rpx;
					text{
						font-size: 28rpx;
						font-weight: bold;
					}
					._top-time{
						font-weight: normal;
						font-size: 22rpx;
						color: $placeholder;
						padding-left: 10rpx;
					}
				}
				._bottom{
					font-size: 22rpx;
					line-height: 22rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
					overflow: hidden;
					color: $placeholder;
				}
			}
			._for-r{
				display: flex;
				align-items: center;
				._red{
					width: 15rpx;
					height: 15rpx;
					background: $red;
					border-radius: 100rpx;
				}
				._del-button{
					font-size: 22rpx;
					line-height: 22rpx;
					margin-left: 15rpx;
					color: $placeholder;
				}
			}
		}
		._for:last-child{
			border-bottom: 0;
		}
		.more-content{
			padding: 25rpx 0;
			font-size: 22rpx;
			line-height: 22rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: $placeholder;
		}
	}
	.all-read-status{
		width: 80rpx;
		height: 80rpx;
		overflow: hidden;
		display: flex;
		align-items: center;
		justify-content: center;
		background: $red;
		line-height: 40rpx;
		position: fixed;
		right: 25rpx;
		bottom: var(--window-bottom);
		margin-bottom: 25rpx;
		border-radius: 100rpx;
		text{
			color: #FFF;
			font-size: 40rpx;
		}
	}
}
</style>
